package com.global.gbinventory.barang.tablemodel;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.global.gbinventory.barang.model.Grup;
import com.global.gbinventory.barang.model.Subgrup;
import com.global.gbinventory.barang.util.TYPE;
import com.global.gbinventory.barang.util.DBMaster;
import com.global.gbinventory.barang.util.CONSTANT;

public class GrupList{
	
	private List<Grup> list; //list to save all grup barang according to DB
	
	private DBMaster dbMaster; //utility to communicate with DB
	private TYPE type;
	
	/**
	 * constructor
	 * @param dbMaster
	 */
	public GrupList(DBMaster dbMaster){
		super();
		this.dbMaster = dbMaster;
		list = new ArrayList<Grup>();
		type = TYPE.GRUPs;
		initialize();
	}
	
	/**
	 * initialize the needed data
	 */
	private void initialize(){
		initGrupList();
	}
	
	/**
	 * initialize the grupList using the data from DB
	 */
	private void initGrupList(){
		List<Object[]> dbGrupTable = dbMaster.getData(type);
		
		for(Object[] obj : dbGrupTable){
			//add data from DB to subgrupList
			int grupId = (Integer) obj[CONSTANT.GRUP_DB_ID];
			String grupNama = obj[CONSTANT.GRUP_DB_NAMA].toString();
			
			List<Object[]> dbSubgrupTable = dbMaster.getSubgrupData(grupId);
			List<Subgrup> subgrups = new ArrayList<Subgrup>();
			for(Object[] sub : dbSubgrupTable){
				
				int subId = (Integer) sub[CONSTANT.SUBGRUP_DB_ID];
				String subNama = sub[CONSTANT.SUBGRUP_DB_NAMA].toString();
				
				Subgrup subgrup = new Subgrup(subId, subNama, grupId);
				subgrups.add(subgrup);
			}
			
			SubgrupTableModel tmSubgrup = new SubgrupTableModel(dbMaster, subgrups, grupId);
			Grup grup = new Grup(grupId, grupNama, tmSubgrup);
			list.add(grup);
			
		}
		
	}
	
	public int getSize(){
		return list.size();
	}
	
	public Object[] getAllGrupNama(){
		Object[] obj = new Object[getSize()];
		for(int i = 0; i < getSize(); i++){
			obj[i] = list.get(i).getNama();
		}
		
		return obj;
	}
	
	public void addNewGrup(String nama){
		
		//add new grup to DB
		try {
			Object[] tuple = new Object[CONSTANT.GRUP_DB_TOTAL_COLUMNS];
			tuple[CONSTANT.GRUP_DB_NAMA - 1] = nama; //because we omitted column id
			dbMaster.addNewTuple(type, tuple);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		//add new grup to grupList
		int id = getNewId();
		Grup grup = new Grup(id, nama, new SubgrupTableModel(dbMaster, new ArrayList<Subgrup>(), id));
		list.add(grup);
	}
	
	public void updateGrup(int index, String newNama){
		Grup grup = list.get(index);
		try {
			Object[] tuple = new Object[CONSTANT.GRUP_DB_TOTAL_COLUMNS];
			tuple[CONSTANT.GRUP_DB_NAMA - 1] = newNama; //because we omitted column id
			dbMaster.updateTuple(type, grup.getId(), tuple);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		grup.setNama(newNama);
	}
	
	public void removeGrup(int index){
		Grup grup = list.get(index);
		try {
			dbMaster.removeTuple(type, grup.getId());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		list.remove(index);
	}
	
	public boolean isExistingGrupNama(String nama){
		for(Grup grup : list){
			if(grup.getNama().equalsIgnoreCase(nama)){
				return true;
			}
		}
		
		return false;
	}
	
	/**
	 * return a new primary key (id) corresponding to 
	 * the autoincrement id in DB
	 * @return
	 */
	private int getNewId(){
		if(!(list.isEmpty())){
			Grup lastGrup = list.get(list.size() - 1);
			int lastId = lastGrup.getId();
			return lastId + 1;
		}else{
			return 1;
		}
		
	}
	
	public SubgrupTableModel getSubgrupTableModel(int index){
		Grup grup = list.get(index);
		return grup.getSubgrupTableModel();
	}
	
}
